package com.example.administrator.myapplication.heart;

import android.app.Application;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.evaluate.TestAssess;
import com.example.administrator.myapplication.tools.History;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


public class heart_main_Fragment extends Fragment {
    private Button btHeartRate;
    private  Button btTimer;
    private Button  bt_heard_submit;
    private BmobApplication heard_apps;
    private Toolbar toolbar;




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_heart_main_, container, false);
        btHeartRate = (Button) view.findViewById(R.id.heart_bt_rate);
        btTimer = (Button) view.findViewById(R.id.heart_bt_time);
        bt_heard_submit= (Button) view.findViewById(R.id.bt_heard_submit);
        heard_apps= (BmobApplication) getActivity().getApplication();

        toolbar = (Toolbar) getActivity().findViewById(R.id.heard_main_toolbar);
        toolbar.setTitle("心肺耐力适能");
        toolbar.setTitleMarginStart(35);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        btHeartRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getHeartRateFragment();

            }
        });

        btTimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getHeartTimerFragment();
            }
        });

        bt_heard_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    heard_apps.setFlag(1);
                    heard_apps.setIntentflag(0);
                int heartscore;     //心率分数
                double VO2;    //最大摄氧量
                int weight = (int)(Math.random()*100);
                int height = (int)(Math.random()*100);
                int age = (int)(Math.random()*100);
                int time = (int)(Math.random()*100);
                int heartRate  = (int)(Math.random()*100);
                int sex;
                if(heard_apps.is_man()){
                    sex = 1;
                }
                else sex = 0;
                heard_apps.setHeartrate(String.valueOf(heartRate));
                    /*最大摄氧量= 132.853－(0.1695×体重)－(0.3877×年龄)＋(6.3150×性别)－(3.2649×T)－(0.1565×心率)。
                        所述体重的单位为千克，年龄的单位为年，性别赋值为女性＝0、男性＝1，时间单位为分钟，心率单位为次/分钟；最大摄氧量 的单位是ml/kg/min。*/
                VO2 = 132.853 - (0.1695 * weight) - (0.3877 * age) + (6.3150 * sex) - (3.2649 * time) - (0.1565 * heartRate);

                if(heard_apps.is_man()){
                    if (VO2 <=  35){
                        heartscore = 0;
                    }
                    else if (VO2 > 35 && VO2 < 58){
                        heartscore = (int) (4.3478 * (VO2 - 35));
                    }
                    else {
                        heartscore = 100;
                    }
                }
                else {
                    if (VO2 <=  26){
                        heartscore = 0;
                    }
                    else if (VO2 > 26 && VO2 < 50){
                        heartscore = (int) (4.1667 * (VO2 - 26));
                    }
                    else {
                        heartscore = 100;
                    }
                }
                    heard_apps.setHeart_result(String.valueOf(heartscore));
                    Intent intent =new Intent(getActivity(), TestAssess.class);
                    getActivity().finish();
                    startActivity(intent);
            }
        });
        return view;
    }

    public void getHeartRateFragment() {
        heart_heartrate_Fragment heartRateFragment = new heart_heartrate_Fragment();
        FragmentManager fm = this.getFragmentManager();            //设置fragment管理器
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_heart, heartRateFragment);      //替换当前的fragment
        ft.commit();
    }

    public void getHeartTimerFragment(){
        heart_timer_Fragment heartTimerFragment = new heart_timer_Fragment();
        FragmentManager fm = this.getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_heart,heartTimerFragment);
        ft.commit();
    }



}
